<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/2/26
 * Time: 14:35
 */
//注册功能

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

require_once 'db.php';
require_once 'response.php';

$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
$userType = isset($_POST['userType']) ? $_POST['userType'] : '';
//$username = isset($_GET['username']) ? $_GET['username'] : '';
//$password = isset($_GET['password']) ? $_GET['password'] : '';
//$userType = isset($_GET['userType']) ? $_GET['userType'] : '';

$data = array(
    'IsSuccess'=> false,
    'InsertId'=> '',
);

//数据判空
if(is_null($username) || is_null($password) || is_null($userType) || ($userType!='0'&&$userType!='1')) {
    return Response::show(401,'数据不合法',$data);
}

//连接数据库
try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
//    $e->getMessage();
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$sql = "INSERT user VALUES(NULL,?,?,?)";
$mysqli_stmt = $connect->prepare($sql);
$mysqli_stmt->bind_param('ssi',$username,$password,$userType);

if($mysqli_stmt->execute()) {
//    var_dump($mysqli_stmt);
    $data['InsertId'] = $mysqli_stmt->insert_id;
    $data['IsSuccess'] = true;
    Response::show('200','注册成功',$data);
} else {
    if($mysqli_stmt->errno=='1062') {
        Response::show('400','注册失败,用户名已被注册',$data);
    } else {
//        var_dump($mysqli_stmt);
        Response::show('404','注册失败,服务器没响应',$data);
    }
}

//释放结果集
$mysqli_stmt->free_result();
//关闭预处理语句
$mysqli_stmt->close();
//关闭连接
$connect->close();




